<!DOCTYPE html>
<html lang="en-us">
  <head>
  <meta http-equiv="content-type" content="text/html;charset=utf-8">
  <meta http-equiv="X-UA-Compatible" content="chrome=1">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta name="robots" content="noodp"/>
  <meta name="author" content="Mr.wei">
  
  
  
  <link rel="prev" href="http://blog.mr-wei.cn/2019/nrf52840%E4%BE%8B%E7%A8%8Bble-app-uart%E6%B7%BB%E5%8A%A0dfu%E5%8A%9F%E8%83%BD/" />
  <link rel="next" href="http://blog.mr-wei.cn/2019/cadence-allegro%E5%88%B6%E4%BD%9Cgerber%E6%96%87%E4%BB%B6/" />
  <link rel="canonical" href="http://blog.mr-wei.cn/2019/nrf52840%E4%BE%8B%E7%A8%8Bble_app_buttonless_dfu%E6%B7%BB%E5%8A%A0%E8%87%AA%E5%AE%9A%E4%B9%89server/" />
  <link rel="apple-touch-icon" sizes="180x180" href="/apple-touch-icon.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/favicon-32x32.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/favicon-16x16.png">
  <link rel="manifest" href="/site.webmanifest">
  <link rel="mask-icon" href="/safari-pinned-tab.svg" color="#5bbad5">
  <meta name="msapplication-TileColor" content="#da532c">
  <meta name="theme-color" content="#ffffff">
  <title>
       
       
           NRF52840例程ble_app_buttonless_dfu添加自定义Server | Mr.wei
       
  </title>
  <meta name="title" content="NRF52840例程ble_app_buttonless_dfu添加自定义Server | Mr.wei">
    
  
  <link rel="stylesheet" href="/font/iconfont.css">
  <link rel="stylesheet" href="/css/main.min.css">


  
  
 

<script type="application/ld+json">
 "@context" : "http://schema.org",
    "@type" : "BlogPosting",
    "mainEntityOfPage": {
         "@type": "WebPage",
         "@id": "http:\/\/blog.mr-wei.cn"
    },
    "articleSection" : "posts",
    "name" : "NRF52840例程ble_app_buttonless_dfu添加自定义Server",
    "headline" : "NRF52840例程ble_app_buttonless_dfu添加自定义Server",
    "description" : "环境  SDK：nRF5_SDK_15.2.0 example：  bootloader:pca10056_ble_debug app:ble_app_uart_c reference app:ble_app_buttonless_dfu  相关硬件  nRF52840-Dongle nRF52840-DK    目的 为SDK中的ble_app_buttonss_dfu例程添加自定义service\n修改过程 工程修改  添加service相关文件  添加.h文件搜索路径  修改ram范围   sdk_config文件修改  使能self service  修改UUID count   main.c文件修改  添加相关头文件  添加service实例化  添加接收处理函数  添加service处理   测试结果 Self_Service文件  C文件\n#include \x26quot;sdk_common.h\x26quot; #if NRF_MODULE_ENABLED(BLE_SELF) #include \x26quot;Self_Service.h\x26quot; #include \x26quot;ble_srv_common.h\x26quot; \/**@brief Function for handling the Write event. * * @param[in] p_self Self Service structure.",
    "inLanguage" : "en-us",
    "author" : "Mr.wei",
    "creator" : "Mr.wei",
    "publisher": "Mr.wei",
    "accountablePerson" : "Mr.wei",
    "copyrightHolder" : "Mr.wei",
    "copyrightYear" : "2019",
    "datePublished": "2019-09-03 10:27:55 \x2b0800 CST",
    "dateModified" : "2019-09-03 10:27:55 \x2b0800 CST",
    "url" : "http:\/\/blog.mr-wei.cn\/2019\/nrf52840%E4%BE%8B%E7%A8%8Bble_app_buttonless_dfu%E6%B7%BB%E5%8A%A0%E8%87%AA%E5%AE%9A%E4%B9%89server\/",
    "wordCount" : "720",
    "keywords" : [ "NRF52840", "Mr.wei"]
}
</script>

</head>

  


  <body class="">
    <div class="wrapper">
        <nav class="navbar">
    <div class="container">
        <div class="navbar-header header-logo">
        	<a href="javascript:void(0);" class="theme-switch"><i class="iconfont icon-xihuan"></i></a>&nbsp;<a href="http://blog.mr-wei.cn">Mr.wei</a>
        </div>
        <div class="menu navbar-right">
                
                
                <a class="menu-item" href="/posts/" title="">Blog</a>
                
                <a class="menu-item" href="http://balance.mr-wei.cn" title="">BalanceCar</a>
                
                <a class="menu-item" href="/categories/" title="">Categories</a>
                
                <a class="menu-item" href="/tags/" title="">Tags</a>
                
                <a class="menu-item" href="/about/" title="About">About</a>
                
        </div>
    </div>
</nav>
<nav class="navbar-mobile" id="nav-mobile" style="display: none">
     <div class="container">
        <div class="navbar-header">
            <div>  <a href="javascript:void(0);" class="theme-switch"><i class="iconfont icon-xihuan"></i></a>&nbsp;<a href="http://blog.mr-wei.cn">Mr.wei</a></div>
            <div class="menu-toggle">
                <span></span><span></span><span></span>
            </div>
        </div>
     
          <div class="menu" id="mobile-menu">
                
                
                <a class="menu-item" href="/posts/" title="">Blog</a>
                
                <a class="menu-item" href="http://balance.mr-wei.cn" title="">BalanceCar</a>
                
                <a class="menu-item" href="/categories/" title="">Categories</a>
                
                <a class="menu-item" href="/tags/" title="">Tags</a>
                
                <a class="menu-item" href="/about/" title="About">About</a>
                
        </div>
    </div>
</nav>







    	 <main class="main">
          <div class="container">
      		
<article class="post-warp" itemscope itemtype="http://schema.org/Article">
    <header class="post-header">
        <h1 class="post-title" itemprop="name headline">NRF52840例程ble_app_buttonless_dfu添加自定义Server</h1>
        <div class="post-meta">
                Written by <a itemprop="name" href="http://blog.mr-wei.cn" rel="author">Mr.wei</a> with ♥ 
                <span class="post-time">
                on <time datetime=2019-09-03 itemprop="datePublished">September 3, 2019</time>
                </span>
                in
                <i class="iconfont icon-folder"></i>
                <span class="post-category">
                        
                </span>
        </div>
    </header>
    <div class="post-content">
        

        
            
        

        
        
     
          
          
          

          
          
          

          

<h1 id="环境">环境</h1>

<ul>
<li>SDK：nRF5_SDK_15.2.0</li>
<li>example：

<ul>
<li>bootloader:pca10056_ble_debug</li>
<li>app:ble_app_uart_c</li>
<li>reference app:ble_app_buttonless_dfu</li>
</ul></li>
<li>相关硬件

<ul>
<li>nRF52840-Dongle</li>
<li>nRF52840-DK
<!-- more --></li>
</ul></li>
</ul>

<h1 id="目的">目的</h1>

<p>为SDK中的ble_app_buttonss_dfu例程添加自定义service</p>

<h1 id="修改过程">修改过程</h1>

<h2 id="工程修改">工程修改</h2>

<ul>
<li><p>添加service相关文件
<figure><img src="/images/ring.svg" data-sizes="auto" data-src="http://pan.mr-wei.cn/image/Blog_image/NRF52840%E4%BE%8B%E7%A8%8Bble_app_buttonless_dfu%E6%B7%BB%E5%8A%A0%E8%87%AA%E5%AE%9A%E4%B9%89Server/13-49-24.png" alt="" class="lazyload"><figcaption class="image-caption"></figcaption></figure></p></li>

<li><p>添加.h文件搜索路径
<figure><img src="/images/ring.svg" data-sizes="auto" data-src="http://pan.mr-wei.cn/image/Blog_image/NRF52840%E4%BE%8B%E7%A8%8Bble_app_buttonless_dfu%E6%B7%BB%E5%8A%A0%E8%87%AA%E5%AE%9A%E4%B9%89Server/13-49-45.png" alt="" class="lazyload"><figcaption class="image-caption"></figcaption></figure></p></li>

<li><p>修改ram范围
<figure><img src="/images/ring.svg" data-sizes="auto" data-src="http://pan.mr-wei.cn/image/Blog_image/NRF52840%E4%BE%8B%E7%A8%8Bble_app_buttonless_dfu%E6%B7%BB%E5%8A%A0%E8%87%AA%E5%AE%9A%E4%B9%89Server/15-20-18.png" alt="" class="lazyload"><figcaption class="image-caption"></figcaption></figure></p></li>
</ul>

<h2 id="sdk-config文件修改">sdk_config文件修改</h2>

<ul>
<li><p>使能self service
<figure><img src="/images/ring.svg" data-sizes="auto" data-src="http://pan.mr-wei.cn/image/Blog_image/NRF52840%E4%BE%8B%E7%A8%8Bble_app_buttonless_dfu%E6%B7%BB%E5%8A%A0%E8%87%AA%E5%AE%9A%E4%B9%89Server/15-22-34.png" alt="" class="lazyload"><figcaption class="image-caption"></figcaption></figure></p></li>

<li><p>修改UUID count
<figure><img src="/images/ring.svg" data-sizes="auto" data-src="http://pan.mr-wei.cn/image/Blog_image/NRF52840%E4%BE%8B%E7%A8%8Bble_app_buttonless_dfu%E6%B7%BB%E5%8A%A0%E8%87%AA%E5%AE%9A%E4%B9%89Server/15-22-58.png" alt="" class="lazyload"><figcaption class="image-caption"></figcaption></figure></p></li>
</ul>

<h2 id="main-c文件修改">main.c文件修改</h2>

<ul>
<li><p>添加相关头文件
<figure><img src="/images/ring.svg" data-sizes="auto" data-src="http://pan.mr-wei.cn/image/Blog_image/NRF52840%E4%BE%8B%E7%A8%8Bble_app_buttonless_dfu%E6%B7%BB%E5%8A%A0%E8%87%AA%E5%AE%9A%E4%B9%89Server/15-23-18.png" alt="" class="lazyload"><figcaption class="image-caption"></figcaption></figure></p></li>

<li><p>添加service实例化
<figure><img src="/images/ring.svg" data-sizes="auto" data-src="http://pan.mr-wei.cn/image/Blog_image/NRF52840%E4%BE%8B%E7%A8%8Bble_app_buttonless_dfu%E6%B7%BB%E5%8A%A0%E8%87%AA%E5%AE%9A%E4%B9%89Server/15-23-28.png" alt="" class="lazyload"><figcaption class="image-caption"></figcaption></figure></p></li>

<li><p>添加接收处理函数
<figure><img src="/images/ring.svg" data-sizes="auto" data-src="http://pan.mr-wei.cn/image/Blog_image/NRF52840%E4%BE%8B%E7%A8%8Bble_app_buttonless_dfu%E6%B7%BB%E5%8A%A0%E8%87%AA%E5%AE%9A%E4%B9%89Server/15-24-4.png" alt="" class="lazyload"><figcaption class="image-caption"></figcaption></figure></p></li>

<li><p>添加service处理
<figure><img src="/images/ring.svg" data-sizes="auto" data-src="http://pan.mr-wei.cn/image/Blog_image/NRF52840%E4%BE%8B%E7%A8%8Bble_app_buttonless_dfu%E6%B7%BB%E5%8A%A0%E8%87%AA%E5%AE%9A%E4%B9%89Server/15-24-13.png" alt="" class="lazyload"><figcaption class="image-caption"></figcaption></figure></p></li>
</ul>

<h2 id="测试结果">测试结果</h2>

<p><figure><img src="/images/ring.svg" data-sizes="auto" data-src="http://pan.mr-wei.cn/image/Blog_image/NRF52840%E4%BE%8B%E7%A8%8Bble_app_buttonless_dfu%E6%B7%BB%E5%8A%A0%E8%87%AA%E5%AE%9A%E4%B9%89Server/15-17-32.png" alt="" class="lazyload"><figcaption class="image-caption"></figcaption></figure></p>

<h2 id="self-service文件">Self_Service文件</h2>

<ul>
<li><p>C文件</p>

<pre><code class="language-C">#include &quot;sdk_common.h&quot;
#if NRF_MODULE_ENABLED(BLE_SELF)
#include &quot;Self_Service.h&quot;
#include &quot;ble_srv_common.h&quot;

/**@brief Function for handling the Write event.
*
* @param[in] p_self     Self Service structure.
* @param[in] p_ble_evt  Event received from the BLE stack.
*/
static void on_write(ble_self_t * p_self, ble_evt_t const * p_ble_evt)
{
ble_gatts_evt_write_t const * p_evt_write = &amp;p_ble_evt-&gt;evt.gatts_evt.params.write;
		static uint8_t Buffer[255];
	  memcpy(Buffer,&amp;p_evt_write-&gt;data[0],p_evt_write-&gt;len);
if (   (p_evt_write-&gt;handle == p_self-&gt;rx_char_handles.value_handle)
    &amp;&amp; (p_evt_write-&gt;len != 0)
    &amp;&amp; (p_self-&gt;rx_handler != NULL))
{
    p_self-&gt;rx_handler(p_ble_evt-&gt;evt.gap_evt.conn_handle, p_self, Buffer,p_evt_write-&gt;len);
}
}

void ble_self_on_ble_evt(ble_evt_t const * p_ble_evt, void * p_context)
{
ble_self_t * p_self = (ble_self_t *)p_context;

switch (p_ble_evt-&gt;header.evt_id)
{
    case BLE_GATTS_EVT_WRITE:
        on_write(p_self, p_ble_evt);
        break;

    default:
        // No implementation needed.
        break;
}
}

uint32_t ble_self_init(ble_self_t * p_self, const ble_self_init_t * p_self_init)
{
uint32_t              err_code;
ble_uuid_t            ble_uuid;
ble_add_char_params_t add_char_params;

// Initialize service structure.
p_self-&gt;rx_handler = p_self_init-&gt;rx_handler;

// Add service.
ble_uuid128_t base_uuid = {LBS_UUID_BASE};
err_code = sd_ble_uuid_vs_add(&amp;base_uuid, &amp;p_self-&gt;uuid_type);
VERIFY_SUCCESS(err_code);

ble_uuid.type = p_self-&gt;uuid_type;
ble_uuid.uuid = LBS_UUID_SERVICE;

err_code = sd_ble_gatts_service_add(BLE_GATTS_SRVC_TYPE_PRIMARY, &amp;ble_uuid, &amp;p_self-&gt;service_handle);
VERIFY_SUCCESS(err_code);

// Add tx characteristic.
memset(&amp;add_char_params, 0, sizeof(add_char_params));
add_char_params.uuid              = LBS_UUID_TX_CHAR;
add_char_params.uuid_type         = p_self-&gt;uuid_type;
add_char_params.init_len          = sizeof(uint8_t);
add_char_params.max_len           = 20;
add_char_params.char_props.read   = 1;
add_char_params.char_props.notify = 1;

add_char_params.read_access       = SEC_OPEN;
add_char_params.cccd_write_access = SEC_OPEN;

err_code = characteristic_add(p_self-&gt;service_handle,
                              &amp;add_char_params,
                              &amp;p_self-&gt;tx_char_handles);
if (err_code != NRF_SUCCESS)
{
    return err_code;
}

// Add rx characteristic.
memset(&amp;add_char_params, 0, sizeof(add_char_params));
add_char_params.uuid             = LBS_UUID_RX_CHAR;
add_char_params.uuid_type        = p_self-&gt;uuid_type;
add_char_params.init_len         = sizeof(uint8_t);
add_char_params.max_len          = 20;
add_char_params.char_props.read  = 1;
add_char_params.char_props.write = 1;

add_char_params.read_access  = SEC_OPEN;
add_char_params.write_access = SEC_OPEN;

return characteristic_add(p_self-&gt;service_handle, &amp;add_char_params, &amp;p_self-&gt;rx_char_handles);
}



uint32_t ble_self_send_message(uint16_t conn_handle, ble_self_t * p_self, uint8_t * message ,uint16_t len)
{
ble_gatts_hvx_params_t params;
memset(&amp;params, 0, sizeof(params));
params.type   = BLE_GATT_HVX_NOTIFICATION;
params.handle = p_self-&gt;tx_char_handles.value_handle;
params.p_data = message;
params.p_len  = &amp;len;

return sd_ble_gatts_hvx(conn_handle, &amp;params);
}
#endif // NRF_MODULE_ENABLED(BLE_SELF)
</code></pre></li>

<li><p>H文件</p>

<pre><code class="language-C">#ifndef __SELF_SERVICE_H__
#define __SELF_SERVICE_H__

#include &lt;stdint.h&gt;
#include &lt;stdbool.h&gt;
#include &quot;ble.h&quot;
#include &quot;ble_srv_common.h&quot;
#include &quot;nrf_sdh_ble.h&quot;

#ifdef __cplusplus
extern &quot;C&quot; {
#endif


/**@brief   Macro for defining a ble_self instance.
*
* @param   _name   Name of the instance.
* @hideinitializer
*/
#define BLE_SELF_DEF(_name)                                                                          \
static ble_self_t _name;                                                                             \
NRF_SDH_BLE_OBSERVER(_name ## _obs,                                                                 \
                 BLE_LBS_BLE_OBSERVER_PRIO,                                                     \
                 ble_self_on_ble_evt, &amp;_name)
	
#define LBS_UUID_BASE        {0x23, 0xD1, 0xBC, 0xEA, 0x5F, 0x78, 0x23, 0x15, \
                          0xDE, 0xEF, 0x12, 0x12, 0x00, 0x00, 0x00, 0x00}
#define LBS_UUID_SERVICE     0x1523
#define LBS_UUID_TX_CHAR 		 0x1524
#define LBS_UUID_RX_CHAR   	 0x1525

// Forward declaration of the ble_self_t type.
typedef struct ble_self_s ble_self_t;	
															
typedef void (*ble_self_rx_handler_t) (uint16_t conn_handle, ble_self_t * p_self, uint8_t* message, uint8_t len);

/** @brief Self Service init structure. This structure contains all options and data needed for
*        initialization of the service.*/
typedef struct
{
ble_self_rx_handler_t rx_handler; /**&lt; Event handler to be called when the rx Characteristic is written. */
} ble_self_init_t;

/**@brief Self Service structure. This structure contains various status information for the service. */
struct ble_self_s
{
uint16_t                    service_handle;      /**&lt; Handle of Self Service (as provided by the BLE stack). */
ble_gatts_char_handles_t    rx_char_handles;    /**&lt; Handles related to the rx Characteristic. */
ble_gatts_char_handles_t    tx_char_handles; /**&lt; Handles related to the tx Characteristic. */
uint8_t                     uuid_type;           /**&lt; UUID type for the Self Service. */
ble_self_rx_handler_t 			rx_handler;   /**&lt; Event handler to be called when the rx Characteristic is written. */
};	

/**@brief Function for initializing the Self Service.
*
* @param[out] p_self     Self Service structure. This structure must be supplied by
*                        the application. It is initialized by this function and will later
*                        be used to identify this particular service instance.
* @param[in] p_self_init  Information needed to initialize the service.
*
* @retval NRF_SUCCESS If the service was initialized successfully. Otherwise, an error code is returned.
*/
uint32_t ble_self_init(ble_self_t * p_self, const ble_self_init_t * p_self_init);															

/**@brief Function for handling the application's BLE stack events.
*
* @details This function handles all events from the BLE stack that are of interest to the Self Service.
*
* @param[in] p_self_evt  Event received from the BLE stack.
* @param[in] p_context   Self Service structure.
*/
void ble_self_on_ble_evt(ble_evt_t const * p_ble_evt, void * p_context);


/**@brief Function for sending a message  notification.
*
' @param[in] conn_handle   Handle of the peripheral connection to which the button state notification will be sent.
* @param[in] p_self        Self Service structure.
* @param[in] message  			notification message.
* @param[in] len  				  message length.
*
* @retval NRF_SUCCESS If the notification was sent successfully. Otherwise, an error code is returned.
*/
uint32_t ble_self_send_message(uint16_t conn_handle, ble_self_t * p_self, uint8_t * message ,uint16_t len);

#ifdef __cplusplus
}
#endif

#endif	//__SELF_SERVICE_H__

</code></pre></li>
</ul>

    </div>

    <div class="post-copyright">
             
            <p class="copyright-item">
                <span>Author:</span>
                <span>Mr.wei </span>
                </p>
            
           
             
            <p class="copyright-item">
                    <span>Link:</span>
                    <a href=http://blog.mr-wei.cn/2019/nrf52840%E4%BE%8B%E7%A8%8Bble_app_buttonless_dfu%E6%B7%BB%E5%8A%A0%E8%87%AA%E5%AE%9A%E4%B9%89server/>http://blog.mr-wei.cn/2019/nrf52840%E4%BE%8B%E7%A8%8Bble_app_buttonless_dfu%E6%B7%BB%E5%8A%A0%E8%87%AA%E5%AE%9A%E4%B9%89server/</span>
            </p>
            
             
            <p class="copyright-item lincese">
                本文采用<a rel="license" href="http://creativecommons.org/licenses/by-nc/4.0/" #target="_blank">知识共享署名-非商业性使用 4.0 国际许可协议</a>进行许可
            </p>
            
    </div>

  
    <div class="post-tags">
        
            <section>
            <i class="iconfont icon-tag"></i>Tag(s): 
            
            <span class="tag"><a href="http://blog.mr-wei.cn/tags/nrf52840/">
                    #NRF52840</a></span>
            
            </section>
        
        <section>
                <a href="javascript:window.history.back();">back</a></span> · 
                <span><a href="http://blog.mr-wei.cn">home</a></span>
        </section>
    </div>

    <div class="post-nav">
        
        <a href="http://blog.mr-wei.cn/2019/nrf52840%E4%BE%8B%E7%A8%8Bble-app-uart%E6%B7%BB%E5%8A%A0dfu%E5%8A%9F%E8%83%BD/" class="prev" rel="prev" title="NRF52840例程ble App Uart添加DFU功能"><i class="iconfont icon-left"></i>&nbsp;NRF52840例程ble App Uart添加DFU功能</a>
         
        
        <a href="http://blog.mr-wei.cn/2019/cadence-allegro%E5%88%B6%E4%BD%9Cgerber%E6%96%87%E4%BB%B6/" class="next" rel="next" title="Cadence Allegro制作Gerber文件">Cadence Allegro制作Gerber文件&nbsp;<i class="iconfont icon-right"></i></a>
        
    </div>

    <div class="post-comment">
          
                 
          
    </div>
</article>
          </div>
		   </main>
      <footer class="footer">
    <div class="copyright">
        &copy;
        
        <span itemprop="copyrightYear">2018 - 2019</span>
        
        <span class="with-love">
    	 <i class="iconfont icon-love"></i> 
         </span>
         
            <span class="author" itemprop="copyrightHolder"><a href="http://blog.mr-wei.cn">Mr.wei</a> | </span> 
         

         
            <a href="http://www.miibeian.gov.cn/" target="_blank" rel="external nofollow">冀ICP备 - 18032379号 </a> |
         
		  <span>Powered by <a href="https://gohugo.io/" target="_blank" rel="external nofollow">Hugo</a> & <a href="https://github.com/liuzc/leaveit" target="_blank" rel="external nofollow">LeaveIt</a></span> 
    </div>

	
<script type="text/x-mathjax-config">
    MathJax.Hub.Config({
        tex2jax: {
            inlineMath: [['$','$'], ['\\(','\\)']],
            displayMath: [['$$','$$'], ['\[','\]']],
            processEscapes: true,
            processEnvironments: true,
            skipTags: ['script', 'noscript', 'style', 'textarea', 'pre'],
            TeX: {
                equationNumbers: { autoNumber: "AMS" },
                extensions: ["AMSmath.js", "AMSsymbols.js"]
            }
        }
    });
</script>
<script type="text/javascript" async src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/MathJax.js?config=TeX-MML-AM_CHTML"></script>
	
	

</footer>












    
     <link href="//lib.baomitu.com/lightgallery/1.6.11/css/lightgallery.min.css" rel="stylesheet">  
      
     <script src="/js/vendor_gallery.min.js" async="" ></script>
    
  



     </div>
  </body>
</html>
